var layerLoad;
var layerNumber;
/**
 * 页面初始化加载
 */
(function(){
	initMemberBankList(1);
})();
function initMemberBankList(currentPage){
    var sysProduct = {
        currentPage : currentPage
    };
	$.ajax({
		url : $api.memberBank.selectMemberRanksByPage,
        type : "POST",
        dataType : "JSON",
        contentType : 'application/json',
        data :JSON.stringify(sysProduct),
		beforeSend : function () {
			layerLoad = layer.load();
		},
		success : function(data) {
			if(!$checkRemouteStatus(data)){
				layer.close(layerLoad);
				return;
			}
			layui.use('laytpl', function() {
				var laytpl = layui.laytpl;
				laytpl(sysProductListTpl.innerHTML).render(data, function(html) {
					$('#system-member-list-body').html(html)
				});
			});
            layui.use('laypage', function() {
                layui.laypage({
                    cont: $('#sys-product-list-body-page'),
                    pages: data.totalPage,
                    groups: 2,
                    curr: data.currentPage,
                    skip: true,
                    skin:'#0e90d2',
                    jump: function (obj,first) {
                        if(!first){
                            initMemberBankList(obj.curr);
                        }
                    }
                });
            });
			layer.close(layerLoad);
		},
		error : function() {
			layer.close(layerLoad);
			layer.msg('网络异常，请稍后再试', {icon: 2});
		}
	});
};
/*编辑*/
var memberRankId="";
function edit(othis){
    memberRankId=$(othis).attr("memberRankId");
    $.ajax({
        url: $api.memberBank.selectMemberRankById + "?memberRankId=" + memberRankId,
        type: "GET",
        dataType: "JSON",
        contentType : 'application/json',
        beforeSend: function () {
            layerLoad = layer.load();
        },
        success: function (data) {
            layer.close(layerLoad);
            var dataObj=data.msg;
            if(data.code == '111111'){
                layer.msg(data.msg, {icon: 2});
            }else{
				/*数据回显*/
                $("#memberRankName").val(dataObj.name);
               	$("#memberRankScale").val(dataObj.scale);
                $("#memberRankAmount").val(dataObj.amount);
            }
        },
        error: function () {
            layer.msg("网络开小差啦", {icon: 2});
        }
    });
    layerNumber = layer.open({
        type : 1,
        title : '会员等级编辑',
        area : 'auto',
        maxWidth : '1000px',
        content : $('#sys-product-update-model'),
        cancel : function(){
            $('#sys-product-form-update')[0].reset();
        }
    });
};
/*保存*/
$("#save").click(function(){
	var productData={};
	/*必填验证*/
    var ajaxFlag=1;
    $('#sys-product-update-model input[required="required"]').each(function(index,dom){
        if($(dom).val()===""){
            $(dom).addClass("am-field-error");
            layer.msg("填写不完整，请检查", {icon: 2});
            ajaxFlag=0;
            return
        }
    });
    if(ajaxFlag==0){
        return
    }
    productData["name"]=$("#memberRankName").val();
    productData["scale"]=$("#memberRankScale").val();
    productData["amount"] = $("#memberRankAmount").val();
    if(memberRankId!==false){
        productData["id"] = memberRankId;
	}
    $.ajax({
        url: $api.memberBank.insertOrUpdateMemberRank,
        type: "POST",
        dataType: "JSON",
        contentType : 'application/json',
        data :  JSON.stringify(productData) ,
        beforeSend: function () {
            layerLoad = layer.load();
        },
        success: function (data) {
            layer.close(layerLoad);
            var dataObj=data.msg;
            if(data.code == '111111'){
                layer.msg(data.msg, {icon: 2});
            }else{
                layer.msg(data.msg, {icon: 1});
                initMemberBankList(1);
            }
            layer.close(layerNumber);
        },
        error: function () {
            layer.close(layerLoad);
            layer.msg("网络开小差啦", {icon: 2});
        }
    });
});
/*新增商品*/
$("#member-add").click(function(){
    memberRankId=false;
    $("#memberRankName").val("");
    $("#memberRankScale").val("");
    $("#memberRankAmount").val("");
    layerNumber = layer.open({
        type : 1,
        title : '会员等级编辑',
        area : 'auto',
        maxWidth : '1000px',
        content : $('#sys-product-update-model'),
        cancel : function(){
            $('#sys-product-form-update')[0].reset();
        }
    });
});

/*非负数限定*/
$("#sys-product-update-model").on("change",".isPositive",function(){
    isPositive(this.value,this);
});
$("#sys-product-update-model").on("keyup",".isPositive",function(){
    isPositive(this.value,this);
});
function isPositive(tet,othis){
    if(tet.length==1){
        tet=tet.replace(/[^1-9]/g,'');
    }else{
        tet=tet.replace(/\D/g,'');
    }
    if(tet.length==1){
        tet=tet.replace(/[^1-9]/g,'');
    }else{
        tet=tet.replace(/\D/g,'');
    }
    $(othis).val(tet);
};

/*memberRankScale 等级限定*/
$("#memberRankScale").on("change",function(){
    isMember(this.value);
});
$("#memberRankScale").on("keyup",function(){
    isMember(this.value);
});
function isMember(text){
    if(text==1){
        $("#memberRankAmount").val("0");
        $("#memberRankAmount").attr("disabled","disabled");
    }else{
        $("#memberRankAmount").removeAttr("disabled");
    }
};